import smtplib
import time
from email.header import Header
from email.mime.text import MIMEText

try:
    # with open('res_html.html', 'r', encoding='utf-8') as f:
    #     res_html = f.read()
    #     re_html = re.findall('href="https://app.*target="_blank"', res_html)
    #     # print(re_html)
    #     # a = 0
    #     for i in re_html:
    #         res_html = res_html.replace(i, '')
    #         # a += 1
    #     # print(a)
    # with open('res_html.html', 'w', encoding='utf-8') as f:
    #     f.write(res_html)
    # 配置信息
    smtp_server = 'mail.nsfocus.com'
    user = 'ar'
    passwd = 'u32Te7sAM'
    # 设置发件人和接收人
    sender = 'ar@nsfocus.com'
    receiver = 'wangqiang@nsfocus.com'
    # receiver = 'yangyue6@nsfocus.com'
    # 主题内容
    # subject = '内容'
    # 邮件主体
    file = open('reply.html', 'r', encoding='utf-8')
    test_repost = file.read()
    file.close()
    m = MIMEText(test_repost, 'html', 'utf-8')
    # 获取邮件主题
    # header = receive_msg.get('Subject', '')
    # header2 = re.findall(r"\?.*?\?", header)
    # 提取邮件主题字符类型
    # char_type = header2[0].replace('?', '')
    # 清洗数据
    # i = header2[1].replace('?', '')
    # i = base64.b64decode(i).decode(char_type)
    # print(i)
    # sub = i + header2[3].replace('?', '')
    # sub = sub.split(':')
    title_type = 'Gartner推荐阅读'
    time_str = time.strftime("%Y%m%d-%H" + ":" + "%M")
    now_sub = time_str + '-' + title_type + '-' + 'Your new Technology Market Essentials research is here'
    print(now_sub)
    # 给发送的邮件添加主题
    m['Subject'] = Header(now_sub, 'utf-8')
    m['From'] = Header('ar@nsfocus.com', 'utf-8')
    # 连接服务器
    smtp = smtplib.SMTP()
    smtp.connect(smtp_server)
    # 登录
    smtp.login(user, passwd)
    # 发送邮件
    smtp.sendmail(sender, receiver, m.as_string())
    smtp.quit()
    print('9--邮件发送成功')
except Exception as e:
    print(e, '9--发送邮件出错')